/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseExceptionDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using System.Diagnostics;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseExceptionDao : BaseDao
    {
        public BaseExceptionDao()
        {
            base.CurrentTableName = BaseExceptionTable.TableName;
        }

        public BaseExceptionDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseExceptionDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string AddEntity(Exception exception)
        {
            string targetValue = BaseSequenceDao.Instance.NewGuid();
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseExceptionTable.TableName);
            builder.SetValue(BaseExceptionTable.FieldID, targetValue);
            builder.SetValue(BaseExceptionTable.FieldMessage, exception.Message);
            builder.SetValue(BaseExceptionTable.FieldThreadName, exception.Source);
            builder.SetValue(BaseExceptionTable.FieldFormattedMessage, exception.StackTrace);
            builder.SetValue(BaseExceptionTable.FieldIPAddress, base.UserInfo.IPAddress);
            builder.SetValue(BaseExceptionTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetValue(BaseExceptionTable.FieldCreateUserRealName, base.UserInfo.Realname);
            builder.SetDBNow(BaseExceptionTable.FieldCreateDate);
            if (builder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return targetValue;
        }

        public BaseExceptionEntity GetEntity(string id)
        {
            return new BaseExceptionEntity(this.Get(id));
        }

        public string LogException(BaseUserInfo userInfo, Exception exception)
        {
            string str = string.Empty;
            IDbHelper dbHelper = new SqlHelper();
            try
            {
                dbHelper.Open();
                str = LogException(dbHelper, userInfo, exception);
            }
            catch (Exception exception2)
            {
                BaseInterfaceLogic.WriteException(exception2);
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public static string LogException(IDbHelper dbHelper, BaseUserInfo userInfo, Exception exception)
        {
            string str = string.Empty;
            if (BaseSystemInfo.LogException)
            {
                if (BaseSystemInfo.EventLog)
                {
                    if (!EventLog.SourceExists(BaseConfiguration.Instance.SoftName))
                    {
                        EventLog.CreateEventSource(BaseConfiguration.Instance.SoftName, BaseConfiguration.Instance.SoftName);
                    }
                    new EventLog { Source = BaseConfiguration.Instance.SoftName }.WriteEntry(exception.Message, EventLogEntryType.Error);
                }
                if (dbHelper.GetDbConnection().State == ConnectionState.Open)
                {
                    str = new BaseExceptionDao(dbHelper, userInfo).AddEntity(exception);
                }
            }
            return str;
        }

        public DataTable Search(string search)
        {
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT *  FROM " + BaseExceptionTable.TableName + " WHERE 1 = 1 ";
            if (!string.IsNullOrEmpty(search))
            {
                string str2 = sqlQuery;
                string str3 = str2 + " AND (" + BaseExceptionTable.FieldIPAddress + " LIKE " + base.DbHelper.GetParameter(BaseExceptionTable.FieldIPAddress);
                string str4 = str3 + " OR " + BaseExceptionTable.FieldFormattedMessage + " LIKE " + base.DbHelper.GetParameter(BaseExceptionTable.FieldFormattedMessage);
                string str5 = str4 + " OR " + BaseExceptionTable.FieldProcessName + " LIKE " + base.DbHelper.GetParameter(BaseExceptionTable.FieldProcessName);
                string str6 = str5 + " OR " + BaseExceptionTable.FieldMachineName + " LIKE " + base.DbHelper.GetParameter(BaseExceptionTable.FieldMachineName);
                sqlQuery = str6 + " OR " + BaseExceptionTable.FieldMessage + " LIKE " + base.DbHelper.GetParameter(BaseExceptionTable.FieldMessage) + ")";
                search = search.Trim();
                if (search.IndexOf("%") < 0)
                {
                    search = "%" + search + "%";
                }
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseExceptionTable.FieldIPAddress, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseExceptionTable.FieldFormattedMessage, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseExceptionTable.FieldProcessName, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseExceptionTable.FieldMachineName, search));
                base.DbHelper.AttachAdapterParameters(base.DbHelper.MakeParameters(BaseExceptionTable.FieldMessage, search));
            }
            DataTable dataTable = new DataTable(BaseNewsTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }
    }
}